package com.frxuexi.user

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import androidx.activity.compose.setContent
import androidx.compose.material3.Scaffold
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.mine.UserIntent
import com.frxuexi.mine.UserRepository
import com.frxuexi.mine.UserState
import com.frxuexi.mine.UserViewModel
import com.frxuexi.mine.entity.User
import com.frxuexi.mine.room.AppDataBase
import com.frxuexi.mine.room.UserViewModelFactory
import com.frxuexi.user.databinding.ActivityLoginBinding
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Route(path = "/user/LoginActivity")
class LoginActivity : AppCompatActivity() {
    lateinit var db: ActivityLoginBinding
    val vm by lazy {
        UserViewModelFactory(UserRepository.getInstance(AppDataBase.getInstance(this)))
            .create(UserViewModel::class.java)
    }

    @SuppressLint("CoroutineCreationDuringComposition")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db = ActivityLoginBinding.inflate(layoutInflater)
        setContentView(db.root)

        setContent {

            val navController = rememberNavController()
            NavHost(navController = navController, startDestination = "login") {
                composable("register") {
                    RegisterPage() { name, pass, pass2 ->
                        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass) || TextUtils.isEmpty(
                                pass2
                            )
                        ) {
                            ToastUtils.showLong("账号和密码都不能为空")
                            return@RegisterPage
                        }
                        if (!RegexUtils.isUsername(name)) {
                            ToastUtils.showLong("不是合法用户名")
                            return@RegisterPage
                        }
                        if (pass != pass2) {
                            ToastUtils.showLong("两次密码不一致")
                            return@RegisterPage
                        }
                        lifecycleScope.launch {
                            vm.channel.send(UserIntent.register(User(uname = name, upass = pass)))
                        }
                    }
                }
                composable("login") {
                    myLogin(navController = navController) { name, pass ->
                        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pass)) {
                            ToastUtils.showLong("账号和密码都不能为空")
                            return@myLogin
                        }

                        /*if(!name.matches(Regex("1[0-9]{10}"))){
                            ToastUtils.showLong("不是合法手机号")
                            return
                        }*/

                        /*if(!name.matches(Regex("[0-9A-z_]{6,12}@[0-9a-z]{2,5}.com"))){
                            ToastUtils.showLong("不是合法邮箱")
                            return
                        }*/
                        if (!RegexUtils.isUsername(name)) {
                            ToastUtils.showLong("不是合法用户名")
                            return@myLogin
                        }

                        lifecycleScope.launch {
//                    vm.channel.send(UserIntent.register(User(uname = name, upass = pass)))
                            vm.channel.send(UserIntent.login(name, pass))
                        }
                    }
                }
            }

            lifecycleScope.launch {
                vm.userState.collect {
                    when (it) {
                        is UserState.onRegisterSuccess -> {
                            navController.popBackStack()
                        }

                        is UserState.onLoginSuccess -> {
                            MMKV.defaultMMKV().encode("name", it.user.uname)
                            ARouter.getInstance().build("/app/MainActivity").navigation()
                            finish()
                        }

                        else -> {}
                    }
                }
            }
        }



        db.loginCom.setContent {

        }
    }
}